package com.android.porttallymansystem.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.porttallymansystem.PTSApplication
import com.android.porttallymansystem.R
import com.android.porttallymansystem.databinding.FragmentWorkBinding
import com.android.porttallymansystem.logic.Repository
import com.android.porttallymansystem.logic.Setting
import com.android.porttallymansystem.ui.adapter.WorkAdapter
import kotlin.concurrent.thread

class WorkFragment : Fragment() {

    private lateinit var binding: FragmentWorkBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentWorkBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onResume() {
        super.onResume()
        initWork()
    }

    private fun initWork() {
        thread {
            val workList = Repository.getWork(Setting.user.value!!.id)
            activity?.runOnUiThread {
                binding.rvWork.layoutManager = LinearLayoutManager(context)
                binding.rvWork.adapter = WorkAdapter(workList)
            }
        }
    }
}